package cn.itcast.contorller.cargo;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.ExtCproductService;
import cn.itcast.service.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping(value = "/cargo/extCproduct")
public class ExtCproductController extends BaseContorller {
    @Reference
    private ExtCproductService extCproductService;//附件的
    @Reference
    private FactoryService factoryService;//工厂的
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @RequestMapping(value = "/list",name = "分页查询 合同下的附件")
    public String findAll(String contractId, String contractProductId, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        ExtCproductExample extCproductExample=new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        /*根据货物id 查 附件*/
        criteria.andContractProductIdEqualTo(contractProductId);
        PageInfo all = extCproductService.findAll(extCproductExample,pageNum,pageSize);
        request.setAttribute("pb",all);
        /*查询附件的工厂*/
        FactoryExample factoryExample=new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        /*页面的参数   货物的id  合同的id*/
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);
        return "/cargo/extc/extc-list";
    }
    @RequestMapping(value = "/edit",name = "新增附件/修改附件")
    public  String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception {
        extCproduct.setCompanyId(company_id);
        extCproduct.setCompanyName(company_name);
        if (UtilFuns.isEmpty(extCproduct.getId())){
            /*新增附件*/
            String upload = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage("http:\\"+upload);
            extCproductService.save(extCproduct);
        }else {
            /*修改附件*/
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    @RequestMapping(value = "/toUpdate", name = "到修改页面")
    public String toUpdate(String id, String contractId, String contractProductId) {
        /*查当前附件*/
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);
        /*所有的附件生产厂家*/
        FactoryExample factoryExample=new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andCtypeEqualTo("附件");
        List<Factory> all = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",all);
        return "cargo/extc/extc-update";
    }
    @RequestMapping(value = "/delete",name = "删除附件")
    public String delete(String id,String contractId,String contractProductId){
    extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
}
